﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace INTROSE
{
    public partial class Edit_User : Form
    {
        SqlConnection con = new SqlConnection("Data Source = .\\SQLEXPRESS; Database = Introse; Integrated Security = True");
        private string name, position, edit;
        private int checker;
        public Edit_User(string name, string position, string edit)
        {
            InitializeComponent();
            this.name = name;
            this.position = position;
            this.edit = edit;
            con.Open();
            if (this.position == "factory worker")
            {
                SqlCommand command1 = new SqlCommand("SELECT * FROM users WHERE userName = '" + name + "'", con);
                SqlDataReader reader1 = command1.ExecuteReader();

                reader1.Read();
                txtUsername.Text = reader1.GetString(1);
                comboPos.Text = reader1.GetString(3);
                txtName.Text = reader1.GetString(4);
                txtAdd.Text = reader1.GetString(5);
                try
                {
                    txtEAdd.Text = reader1.GetString(6);
                }
                catch { ArgumentNullException e; }
                comboPos.Enabled = false;
                reader1.Close();
            }
            else
            {
                SqlCommand command2 = new SqlCommand("SELECT * FROM users WHERE uName = '" + edit + "'", con);
                SqlDataReader reader3 = command2.ExecuteReader();

                reader3.Read();
                txtUsername.Text = reader3.GetString(1);
                comboPos.Text = reader3.GetString(3);
                txtName.Text = reader3.GetString(4);
                txtAdd.Text = reader3.GetString(5);
                txtEAdd.Text = reader3.GetString(6);
                comboPos.Items.Add("president");
                comboPos.Items.Add("vice-president");
                comboPos.Items.Add("secretary");
                comboPos.Items.Add("factory worker");
                comboPos.Enabled = true;
                reader3.Close();
            }
            con.Close();
        }

        private void btnCancel_Click_1(object sender, EventArgs e)
        {
            if (this.position == "factory worker")
            {
                this.Close();
                MenuProductionStaff menu = new MenuProductionStaff(name, position);
                menu.Show();
            }
            else
            {
                this.Close();
                Input_Employee_Name inputEmpName = new Input_Employee_Name(name, position);
                inputEmpName.Show();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            con.Open();
            if (this.position == "factory worker")
            {
                SqlCommand command = new SqlCommand("select * from users where userName = '" + this.edit + "'", con);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                int a = reader.GetInt32(0);
                if (!txtOldPw.Text.Equals(reader.GetString(2)))
                {
                    MessageBox.Show("Incorrect Old Password!");
                    this.checker = 0;
                }
                else if (!txtConfirmPw.Text.Equals(txtNewPw.Text))
                {
                    MessageBox.Show("The entered passwords doesn't match with each other! Please enter correctly!");
                    this.checker = 0;
                }
                else if (txtAdd.Text == "" || txtConfirmPw.Text == "" || txtEAdd.Text == "" || txtName.Text == "" || txtNewPw.Text == "" || txtOldPw.Text == "" || txtUsername.Text == "")
                {
                    MessageBox.Show("Please fill up the empty fields, unchecked boxes and/or unclicked buttons");
                    this.checker = 0;
                }
                else
                    this.checker = 1;
                if (this.checker != 0)
                {
                    SqlCommand command3 = new SqlCommand("UPDATE users SET uName = '" + txtUsername.Text + "', psWord = '" + txtNewPw.Text + "', position = '" + comboPos.Text + "', userName = '" + txtName.Text + "', [add] = '" + txtAdd.Text + "', email = '" + txtEAdd.Text + "' where ctNo = '" + a + "'", con);
                    reader.Close();
                    command3.ExecuteNonQuery();
                    MessageBox.Show("Successfully updated a user!");
                }
            }
            else
            {
                SqlCommand command = new SqlCommand("select * from users where uName = '" + this.edit + "'", con);
                SqlDataReader reader = command.ExecuteReader();
                reader.Read();
                int a = reader.GetInt32(0);
                if (!txtOldPw.Text.Equals(reader.GetString(2)))
                {
                    MessageBox.Show("Incorrect Old Password!");
                    this.checker = 0;
                }
                else if (!txtConfirmPw.Text.Equals(txtNewPw.Text))
                {
                    MessageBox.Show("The entered passwords doesn't match with each other! Please enter correctly!");
                    this.checker = 0;
                }
                else if (txtAdd.Text == "" || txtConfirmPw.Text == "" || txtEAdd.Text == "" || txtName.Text == "" || txtNewPw.Text == "" || txtOldPw.Text == "" || txtUsername.Text == "")
                {
                    MessageBox.Show("Please fill up the empty fields, unchecked boxes and/or unclicked buttons");
                    this.checker = 0;
                }
                else
                    this.checker = 1;
                if (this.checker != 0)
                {
                    SqlCommand command3 = new SqlCommand("UPDATE users SET uName = '" + txtUsername.Text + "', psWord = '" + txtNewPw.Text + "', position = '" + comboPos.Text + "', userName = '" + txtName.Text + "', [add] = '" + txtAdd.Text + "', email = '" + txtEAdd.Text + "' where ctNo = '" + a + "'", con);
                    reader.Close();
                    command3.ExecuteNonQuery();
                    MessageBox.Show("Successfully updated your account!");
                }
            }
            con.Close();
            if (this.checker == 1)
            {
                if (this.position == "factory worker")
                {
                    this.Close();
                    MenuProductionStaff menu = new MenuProductionStaff(name, position);
                    menu.Show();
                }
                else
                {
                    this.Close();
                    MenuExecutive menu2 = new MenuExecutive(name, position);
                    menu2.Show();
                }
            }

        }

        private void Edit_User_Load(object sender, EventArgs e)
        {

        }
    }
}
